package org.bookie.framework.services.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.hivemind.util.Defense;
import org.apache.tapestry.engine.IEngineService;
import org.apache.tapestry.engine.ILink;
import org.apache.tapestry.error.RequestExceptionReporter;
import org.apache.tapestry.services.LinkFactory;
import org.apache.tapestry.services.ServiceConstants;
import org.apache.tapestry.web.WebRequest;
import org.apache.tapestry.web.WebResponse;

public abstract class CommonEngineService implements IEngineService {
	/**
	 * Injected
	 */
	LinkFactory linkFactory;

	/**
	 * Injected
	 */
	WebRequest request;

	/**
	 * Injected
	 */
	WebResponse response;

	/**
	 * Injected
	 */
	RequestExceptionReporter exceptionReporter;
	
	/**
	 * Copied from RounderCorners service
	 */
	@SuppressWarnings("unchecked")
	public ILink getLink(boolean post, Object parameter) {
		Defense.notNull(parameter, "parameter");
		Defense.isAssignable(parameter, Object[].class, "parameter");

		Object[] parms = (Object[]) parameter;

		Map parameters = new HashMap();
		parameters.put(ServiceConstants.SERVICE, getName());
		parameters.put(ServiceConstants.PARAMETER, parms);

		return linkFactory.constructLink(this, post, parameters, false);
	}
	
	/**
	 * @category injector
	 */
	public void setLinkFactory(LinkFactory linkFactory) {
		this.linkFactory = linkFactory;
	}

	/**
	 * @category injector
	 * @param request
	 */
	public void setRequest(WebRequest request) {
		this.request = request;
	}

	/**
	 * @category injector
	 * @param response
	 */
	public void setResponse(WebResponse response) {
		this.response = response;
	}
	

	/**
	 * @category injected
	 * @param exceptionReporter
	 */
	public void setExceptionReporter(RequestExceptionReporter exceptionReporter) {
		this.exceptionReporter = exceptionReporter;
	}


}